package com.seecen.myspringboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.seecen.myspringboot.entity.Book;
import com.seecen.myspringboot.entity.BookCategory;
import com.seecen.myspringboot.service.BookCategoryService;
import com.seecen.myspringboot.dao.BookCategoryDao;
import com.seecen.myspringboot.service.BookService;
import com.seecen.myspringboot.vo.MyPage;
import com.seecen.myspringboot.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author bigpig
* @description 针对表【T_BOOK_CATEGORY】的数据库操作Service实现
* @createDate 2024-08-26 16:05:56
*/
@Service
public class BookCategoryServiceImpl extends ServiceImpl<BookCategoryDao, BookCategory>
    implements BookCategoryService{

    @Autowired
    private BookService bookService;

    @Transactional
    public Result delete(int id) {
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("book_category_id", id);
        // 根据图书分类，查找图书
        List<Book> list = bookService.list(queryWrapper);
        if(list == null || list.isEmpty()) {
            this.removeById(id);
            return Result.success();
        }
        return Result.fail("图书分类下存在图书，不能删除");
    }

    @Override
    public void findByParam(MyPage<BookCategory> myPage) {
        QueryWrapper<BookCategory> queryWrapper = new QueryWrapper<>();
        if(myPage != null) {
            BookCategory bookCategory = myPage.getParam();
            if (bookCategory != null) {
                if (bookCategory.getBookCategoryId() != null) {
                    queryWrapper.eq("book_category_id", bookCategory.getBookCategoryId());
                }
                if (!"".equals(bookCategory.getBookCategoryName())) {
                    queryWrapper.like("book_category_name", bookCategory.getBookCategoryName());
                }
            }
        }
        // 设置分页
        PageHelper.startPage(myPage.getPageNum(), myPage.getPageSize());
        Page pageHelperPage = (Page) this.list(queryWrapper);

        myPage.setTotal(pageHelperPage.getTotal());
        myPage.setDatas(pageHelperPage.getResult());
        myPage.setPages(pageHelperPage.getPages());
        myPage.setPageSize(pageHelperPage.getPageSize());
        myPage.setPageNum(pageHelperPage.getPageNum());
    }
}




